<?php
declare(strict_types=1);
namespace app;
class Sms
{
    /**
     * 发送短信
     * @param string $mobile 手机号
     * @param string $content 短信内容
     * @return bool
     */
    public function send(string $mobile, string $content)
    {
        // 发送短信
        // 发送一对多短信接口
        $apiURL = 'http://factor.yumudun.com/apiServer/v1/sms/sendMessageMore';
        // 用户ID
        $userId = '545766326292794';
        // 用户私钥
        $privateKey = '99779fe4-00bb-4ccd-8912-ee2e40b68798';
        // 时间戳
        $time = time() * 1000;
        // 请求头
        $header = [
            'Content-Type'  => 'application/json',
            'userid'        => $userId,
            'timestamp'     => $time,
            'sign'          => md5($time .'|'. $privateKey)
        ];
        // 请求体
        $body = json_encode([
            'phoneList' => [$mobile],
            'content'   => $content
        ]);
        // 请求短信接口
        $data = $this->sendPostRequest($apiURL, $body, $header);

        return $data;
        // 请求封状
        
    }
    function sendPostRequest($url, $opt = [], $header = [])
    {
        $handle = curl_init();
        array_walk($header, function(&$val, $key) {
            $val = $key .':'. $val;
        });
        curl_setopt($handle, CURLOPT_HTTPHEADER, array_values($header));
        curl_setopt($handle, CURLOPT_URL, $url);
        curl_setopt($handle, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($handle, CURLOPT_POST, true);
        curl_setopt($handle, CURLOPT_POSTFIELDS, is_array($opt) ? http_build_query($opt) : $opt);
        header('Content-Type:text/html; charset=utf8;');
        $data = curl_exec($handle);
        if ($data === false) {
            $data = curl_error($handle);
            curl_close($handle);
            throw new Exception($data);
        }
        curl_close($handle);
        return $data;
    }
}